import isFunction from "./isFunction";
import isFunctionComponent from "./isFunctionComponent";
import mountNativeElement from "./mountNativeElement";

export default function mountComponent(virtualDOM,container){
    
    let nuxtVirtualDOM = null
    //判断是类组件还是函数组件
  if(isFunctionComponent(virtualDOM)){
      //函数组件调用buildFunctionComponent进行函数处理
    nuxtVirtualDOM = buildFunctionComponent(virtualDOM)
  }else{
      //类组件
    nuxtVirtualDOM = buildClassComponent(virtualDOM)
  }
  //判断得到的VirtualDOM 是否是组件
  if(isFunction(nuxtVirtualDOM)){
      //如果是继续调用mountComponent 解刨组件
      mountComponent(nuxtVirtualDOM,container)
  }else{
      // 如果是 Navtive Element 就去渲染
    mountNativeElement(nuxtVirtualDOM,container)
  }
  
}

function buildFunctionComponent(virtualDOM){
   return virtualDOM.type(virtualDOM.props || {})
}
function buildClassComponent(virtualDOM){
   //实例化类组件 得到类组件实例对象 并将props传递给类组件
  const component = new virtualDOM.type(virtualDOM.props || {})
  //调用组件的render方法 得到要渲染VirtualDOM
  const nuxtVirtualDOM = component.render()
 //返回VirtualDOM
  return nuxtVirtualDOM
} 